/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * BasePropertyController
 * 基础字段信息
 * 修改纪录
 * 2020-12-01 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2020-12-01
 */
package com.topscomm.base.controller;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.controller.auto.BasePropertyControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: zhanglongfei
 * @date: 2020-12-01
 * @description:基础字段信息Controller
 * @modify:
 */
@RestController
@RequestMapping("base/property")
public class BasePropertyController extends BasePropertyControllerAuto implements IBasePropertyController {
	@Autowired
	RedisTemplate redisTemplate;

	/**
	 * @author: zhanglongfei
	 * @date: 2020-12-01
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: zhanglongfei
	 * @date: 2020-12-01
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description: 删除指定键值的redis缓存
	 * @param paraMap
	 * {
	 * rediskey："XXXX" //键值 必填
	 * }
	 * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @author: wangxinhua
	 * @date: 2020/12/4 17:20
	 * @modify:
	 */
	@DeleteMapping(value = "/clearRedis")
	public ResponseResult<Object> clearRedis(@RequestBody(required = true) Map<String, Object> paraMap) {
		try {
			if (!paraMap.containsKey("rediskey") || StringUtil.isEmpty(paraMap.get("rediskey").toString())) {
				return ResponseResult.error("rediskey不能为空!");
			}
			String key = paraMap.get("rediskey").toString();
			Set<String> keys = redisTemplate.keys(key);
			if (keys.size() > 0) {
				for (String item : keys) {
					this.redisTemplate.delete(item);
				}
				return ResponseResult.ok("清除成功!");
			} else {
				return ResponseResult.error(key + "键值在redis中不存在!");
			}
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @param paraMap
	 * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @description: 获取客户信息完整度
	 * @author: wangxinhua
	 * @date: 2020/12/3 15:31
	 * @modify:
	 */
	@GetMapping(value = "/calculateCompleteness")
	public ResponseResult<Object> getCalculateCompleteness(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			BigDecimal culateCompleteness = this.service.calculateCompleteness(paraMap.get("sourceid").toString(), paraMap.get("sourcetype").toString());
			return ResponseResult.ok("成功!", culateCompleteness);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}
}
